package za.co.smartmi.service;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.logging.Level;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import za.co.smartmi.service.exception.ServiceExceptionFactory;
import za.co.smartmi.util.AppConstants;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import za.co.smartmi.dao.ClientDao;
import za.co.smartmi.dao.SystemUserDao;
import za.co.smartmi.dao.SystemUserRoleDao;
import za.co.smartmi.dao.exceptions.ClientDaoException;
import za.co.smartmi.dao.exceptions.SystemUserDaoException;
import za.co.smartmi.dao.exceptions.SystemUserRoleDaoException;
import za.co.smartmi.form.ClientSearchForm;
import za.co.smartmi.model.Client;
import za.co.smartmi.model.SystemAccount;
import za.co.smartmi.model.SystemUser;
import za.co.smartmi.model.SystemUserRole;

import za.co.smartmi.service.exception.ServiceException;
import za.co.smartmi.util.ConverterUtil;
import za.co.smartmi.util.ResourcesUtil;

@Service
public class ClientServiceImpl implements ClientService {

    private static final Logger log = Logger.getLogger(ClientServiceImpl.class);
    @Autowired
    private ClientDao clientDao;

    @Transactional
    public List<Client> getClientsBySearch(ClientSearchForm clientSearchForm) throws ServiceException {

        List<Client> clients;
        try {
            if (!StringUtils.isEmpty(clientSearchForm.getClientid())) {
                clients = clientDao.findWhereClientidEquals(new Integer(clientSearchForm.getClientid()));
            } else {
                Client search = ConverterUtil.convertClientSearchToClient(clientSearchForm);
                clients = clientDao.findClientsBySearchCriteria(search);
            }

        } catch (ClientDaoException e) {
            throw ServiceExceptionFactory.createServiceException(AppConstants.ERR_SEARCH_CLIENT, e);
        }
//        if (clients == null || clients.size()==0) {
//            throw ServiceExceptionFactory.createServiceException(AppConstants.ERR_SEARCH_CLIENT_NOT_FOUND, null);
//        }
        return clients;

    }

    public Client getClient(String clientId) throws ServiceException {

        Client client;

        try {
            client = clientDao.findByPrimaryKey(new Integer(clientId));
        } catch (ClientDaoException ex) {
            throw ServiceExceptionFactory.createServiceException(AppConstants.ERR_GET_CLIENT, ex);
        }
        if (client == null) {
            final Throwable throwable = new RuntimeException(ResourcesUtil.getInstance().getProperty(AppConstants.ERR_GET_CLIENT_NOT_EXIST));
            throw ServiceExceptionFactory.createServiceException(AppConstants.ERR_GET_CLIENT_NOT_EXIST, throwable);
        }
        return client;
    }

    
}
